﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorkFlowBLL;

namespace QLCF.Forms
{
    public partial class frmQuantification : Form
    {
        public int permission=0;
        private bool add = false;
        private bool edit = false;
        private bool delete = false;
        public frmQuantification()
        {
            InitializeComponent();
            permission = CtrAdmin.GetPermission(Program.UserEntity.Permission, Config.Enum.Function.frmQuantification);
            add = ((permission & (int)Config.Enum.Permission.Add) == (int)Config.Enum.Permission.Add);
            edit = ((permission & (int)Config.Enum.Permission.Edit) == (int)Config.Enum.Permission.Edit);
            delete = ((permission & (int)Config.Enum.Permission.Delete) == (int)Config.Enum.Permission.Delete);
            lstViewCate_LoadData();
        }       

        private ListViewItem itemlstVQuantifi = null;
        public void UpdateQuantity(string quantity)
        {
            if (itemlstVQuantifi != null)
                itemlstVQuantifi.SubItems[1].Text = quantity.ToString();
        }
        private void lstViewCate_LoadData()
        {
            TreeNode node1 = new TreeNode();
            node1.Text = "Tất cả";
            node1.Tag = -1;
            node1.ImageIndex = 1;
            node1.SelectedImageIndex = 1;
            treeViewCate.Nodes.Add(node1);
            CtrCategory ctrC = new CtrCategory();
            var items = ctrC.GetListCategory();
            foreach (var item in items)
            {
                TreeNode node = new TreeNode();
                node.Text=item.CategoryName;
                node.Tag=item.CategoryID;
                node.ImageIndex = 0;
                treeViewCate.Nodes.Add(node);
            }
        }
                

        private void contextMenuStripQuantification_Opening(object sender, CancelEventArgs e)
        {
            if (lstViewQuatifycation.SelectedItems.Count != 1)
            {
                editToolStripMenuItem.Enabled = false;
                deleteToolStripMenuItem.Enabled = false;
            }
            else
            {
                editToolStripMenuItem.Enabled = edit;
                deleteToolStripMenuItem.Enabled = delete;
            }
            if(treeViewPro.SelectedNode==null)
                addNewToolStripMenuItem.Enabled = false;
            else
                addNewToolStripMenuItem.Enabled = add;
        }
        private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstViewQuatifycation.SelectedItems.Count != 1)
                return;
            if (MessageBox.Show("Bạn có muốn xóa nguyên liệu này không?", "Xác nhận!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                var item= lstViewQuatifycation.SelectedItems[0];
                int quantifiID = Convert.ToInt32(item.Tag);
                CtrProduct ctrp = new CtrProduct();
                if (ctrp.QuantificationDelete(quantifiID) > 0)
                {
                    CtrAdmin ctra = new CtrAdmin();
                    ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 3, "Xóa nguyên liệu \"" + item.Text + "\" khỏi định lượng mặt hàng \"" + treeViewPro.SelectedNode.Text + "\"","Định lượng");
                    lstViewQuatifycation.Items.Remove(item);
                }
                else
                    MessageBox.Show("Có lỗi xảy ra!");
            }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstViewQuatifycation.SelectedItems.Count != 1)
                return;
            var item= lstViewQuatifycation.SelectedItems[0];
            itemlstVQuantifi = item;
            int quantifiID = Convert.ToInt32(item.Tag);
            CtrProduct ctrp = new CtrProduct();
            var quanInfo=ctrp.QuantificationGetInfo(quantifiID);
            popupEditQuantification editQuan = new popupEditQuantification(this, quanInfo.QuantificationID,quanInfo.ProductChildName, (float)quanInfo.Quantity, quanInfo.Unit);
            editQuan.ShowDialog();
        }

        private void treeViewCate_AfterSelect(object sender, TreeViewEventArgs e)
        {
            pbLoadingPro.Visible = true;
            treeViewPro.Nodes.Clear();
            var cateID = Convert.ToInt32(e.Node.Tag);
            CtrProduct ctrT = new CtrProduct();
            var items = ctrT.ProductGetByCate(cateID, -1,1);
            foreach (var item in items)
            {
                TreeNode node = new TreeNode();
                node.Text = item.ProductName;
                node.Tag = item.ProductID;
                node.ImageIndex = 0;
                treeViewPro.Nodes.Add(node);
            }
            e.Node.BackColor = Color.FromArgb(220, 220, 220);
            pbLoadingPro.Visible = false;
        }

        private void treeViewPro_AfterSelect(object sender, TreeViewEventArgs e)
        {
            pbLoadingQuantifi.Visible = true;
            lstViewQuatifycation.Items.Clear();
            var pID = Convert.ToInt32(e.Node.Tag);
            CtrProduct ctrT = new CtrProduct();
            var items = ctrT.ProductGetQuantification(pID);
            foreach (var item in items)
            {
                var lItem = lstViewQuatifycation.Items.Add(item.ProductChildName);
                lItem.ImageIndex = 0;
                lItem.Tag = item.QuantificationID;
                lItem.SubItems.Add(item.Quantity + " " + item.Unit);
            }
            e.Node.BackColor = Color.FromArgb(220, 220, 220);
            pbLoadingQuantifi.Visible = false;
        }

        public void ReloadQuantification()
        {
            lstViewQuatifycation.Items.Clear();
            var pID = Convert.ToInt32(treeViewPro.SelectedNode.Tag);
            CtrProduct ctrT = new CtrProduct();
            var items = ctrT.ProductGetQuantification(pID);
            foreach (var item in items)
            {
                var lItem = lstViewQuatifycation.Items.Add(item.ProductChildName);
                lItem.ImageIndex = 0;
                lItem.Tag = item.QuantificationID;
                lItem.SubItems.Add(item.Quantity + " " + item.Unit);
            }
        }

        private void frmQuantification_Load(object sender, EventArgs e)
        {
            splitContainer1.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
            // Why isn't there a convenient enum value for 'All'? 
            splitContainer1.Location = new Point(0, 20);
            splitContainer1.Size = new Size(splitContainer1.Parent.ClientSize.Width
             , splitContainer1.Parent.ClientSize.Height - (0 +20)
             );
            timer1.Interval = 1;
            timer1.Start();
        }

        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            popupAddNewProIntoQuantification frmAdd = new popupAddNewProIntoQuantification(Convert.ToInt32(treeViewPro.SelectedNode.Tag), treeViewPro.SelectedNode.Text, this);
            frmAdd.ShowDialog();
        }

        private void treeViewCate_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (treeViewCate.SelectedNode != null)
                treeViewCate.SelectedNode.BackColor = Color.Transparent;
        }

        private void treeViewPro_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (treeViewPro.SelectedNode != null)
                treeViewPro.SelectedNode.BackColor = Color.Transparent;
        }

        private void contextMenuStripNewInPro_Opening(object sender, CancelEventArgs e)
        {
            if (treeViewPro.SelectedNode==null)
            {
                AddQuantifiFromPToolStripMenuItem.Enabled = false;
            }
            else
            {
                AddQuantifiFromPToolStripMenuItem.Enabled = add;
            }
        }

        private void AddQuantifiFromPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            popupAddNewProIntoQuantification frmAdd = new popupAddNewProIntoQuantification(Convert.ToInt32(treeViewPro.SelectedNode.Tag), treeViewPro.SelectedNode.Text, this);
            frmAdd.ShowDialog();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            timer1.Stop();
        }
        

        private void lstViewQuatifycation_DoubleClick(object sender, EventArgs e)
        {
            if (lstViewQuatifycation.SelectedItems.Count != 1)
                return;
            var item = lstViewQuatifycation.SelectedItems[0];
            itemlstVQuantifi = item;
            int quantifiID = Convert.ToInt32(item.Tag);
            CtrProduct ctrp = new CtrProduct();
            var quanInfo = ctrp.QuantificationGetInfo(quantifiID);
            popupEditQuantification editQuan = new popupEditQuantification(this, quanInfo.QuantificationID, quanInfo.ProductChildName, (float)quanInfo.Quantity, quanInfo.Unit);
            editQuan.ShowDialog();
        }

        
    }
}
